package com.ocom.company.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.company.EntityDeviceUserBind;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.company.dao.IDaoDeviceUserBind;
import com.ocom.company.service.DeviceUserBindService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DeviceUserBindServiceImpl extends ServiceImpl<IDaoDeviceUserBind, EntityDeviceUserBind> implements DeviceUserBindService {


    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityDeviceUserBind> mainAccountList(Long comId, String deviceId) {
        QueryWrapper<EntityDeviceUserBind> wrapper = Wrappers.query();
        wrapper.eq("device_id", deviceId)
                .select("user_id", "device_id", "user_name", "company_name", "merchant_name");
        return this.list(wrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public String switchAccount(Long comId, String userId, String deviceId) {
        QueryWrapper<EntityDeviceUserBind> wrapper = Wrappers.query();
        wrapper.eq("user_id", userId)
                .eq("device_id", deviceId)
                .select("login_json");
        return this.getOne(wrapper).getLoginJson();
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.MASTER)
    public void addBindDeviceUserBind(Long companyId, EntityDeviceUserBind entityDeviceUserBind) {
        UpdateWrapper<EntityDeviceUserBind> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",entityDeviceUserBind.getUserId());
        this.saveOrUpdate(entityDeviceUserBind,updateWrapper);
    }


}
